/*
 * Copyright 2006 Kevin Wetzels 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0 
 *       
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package be.roam.drest.service.youtube.xml;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import be.roam.drest.service.youtube.YouTubeUserProfile;
import be.roam.util.StringUtil;

/**
 * Handler for a YouTube XML response for a friends list.
 */
public class YouTubeFriendHandler extends YouTubeResponseHandler {
    
    /**
     * The list of parsed friends.
     */
    private List<YouTubeUserProfile> friendList;
    
    /**
     * The current friend being parsed.
     */
    private YouTubeUserProfile currentFriend;
    
    /**
     * Gets the list of friends.
     * 
     * @return list of friends.
     */
    public List<YouTubeUserProfile> getFriendList() {
        return friendList;
    }
    
    /* (non-Javadoc)
     * @see be.roam.drest.service.youtube.xml.YouTubeResponseHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (!isErrorResponse()) {
            if ("friend_list".equals(localName)) {
                friendList = new ArrayList<YouTubeUserProfile>();
            } else if ("friend".equals(localName)) {
                currentFriend = new YouTubeUserProfile();
                friendList.add(currentFriend);
            } else if ("user".equals(localName) || "video_upload_count".equals(localName) || "favorite_count".equals(localName)
                    || "friend_count".equals(localName)) {
                characterBuffer = new StringBuilder();
            } else {
                super.startElement(uri, localName, qName, attributes);
            }
        } else {
            super.startElement(uri, localName, qName, attributes);
        }
    }

    /* (non-Javadoc)
     * @see be.roam.drest.service.youtube.xml.YouTubeResponseHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (!isErrorResponse()) {
            if ("user".equals(localName)) {
                currentFriend.setUserName(getCharacters(true));
            } else if ("video_upload_count".equals(localName)) {
                currentFriend.setNrVideosUploaded(StringUtil.parseInt(getCharacters(true)));
            } else if ("favorite_count".equals(localName)) {
                currentFriend.setNrFavouriteVideos(StringUtil.parseInt(getCharacters(true)));
            } else if ("friend_count".equals(localName)) {
                currentFriend.setNrFriends(StringUtil.parseInt(getCharacters(true)));
            } else if ("friend".equals(localName)) {
                currentFriend = null;
            } else {
                super.endElement(uri, localName, qName);
            }
        } else {
            super.endElement(uri, localName, qName);
        }
    }    

}
